package cc.vv.baselibrary.util.remind;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;

import cc.vv.baselibrary.db.daTable.RemindFunctionObj;
import cc.vv.baselibrary.server.RemindFunctionRelatedServer;
import cc.vv.baselibrary.util.BaseTimeUtil;
import cc.vv.baselibrary.util.LKAppUtil;
import cc.vv.baselibrary.util.LKLogUtil;

/**
 * 闹钟响应广播
 * Created by sun.li on 2017/9/15.
 */

public class MyAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("lx_log", "onReceive: 收到闹钟提醒啦......" + System.currentTimeMillis());
        if (intent == null) {
            return;
        }
        if ("cc.vv.btpartyjob.MyAlarm".equals(intent.getAction())) {
            Log.d("lx_log", "onReceive: 收到闹钟提醒" + intent.getAction());
            RemindFunctionObj remindFunctionObj = null;
            try {
                remindFunctionObj = (RemindFunctionObj) intent.getSerializableExtra("Remind");
                Log.d("lx_log", "onReceive: " + remindFunctionObj.toString());
            } catch (Exception e) {
                Log.d("lx_log", "onReceive: " + e.getMessage());
            }
            if (remindFunctionObj == null) {
                return;
            }
            String title = remindFunctionObj.getRemindTitle();
            String startTime = remindFunctionObj.getRemindStartTime();
            Log.d("lx_log", "MyAlarm-" + "title:" + title + "  startTime:" + startTime);
            if (!TextUtils.isEmpty(title)) {
                showRemind(context, title, startTime, remindFunctionObj);
            }
        } else {
            System.out.println("repeating alarm");
        }
    }

    /**
     * 显示提醒
     */
    private void showRemind(Context context, String title, String startTime, RemindFunctionObj remindFunctionObj) {
        if (!TextUtils.isEmpty(startTime)) {
            String content = "伯图健康提醒您，该" + title + "了";
            if ("喝水提醒".equals(title)) {
                content = "你与健康之间，还差一杯水，干了吧；";
            } else if ("久坐提醒".equals(title)) {
                content = "今天坐了太久啦，起身活动一下吧；";
            }
            if (LKAppUtil.getInstance().isAppOnForeground()) {
                //弹窗
                Intent intent = new Intent(context, ClockAlarmActivity.class);
                intent.putExtra("title", title);
                intent.putExtra("data", remindFunctionObj);
                intent.putExtra("content", content);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else {
                //发送通知
                new BCNotification(context.getApplicationContext()).postNotification(title, content);
            }
        }
    }
}
